package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.db.UserStatus;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserStatusApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {
    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private PublishApi publishApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MovementsMQService mqService;
    @Reference
    private UserStatusApi userStatusApi;

    public ResponseEntity findCommentsList(String movementId, int page, int pagesize) {
        //查询动态id对应的评论信息
        PageResult<Comment> pageResult = commentApi.findComment(movementId, page, pagesize);
        //转换成vo
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : pageResult.getItems()) {
            CommentVO vo = new CommentVO();
            //把评论信息封装到vo中
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            vo.setHasLiked(0);
            //封装评论人信息
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());

            voList.add(vo);
        }
        PageResult<CommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);

        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity saveComment(String publishId, String content) {
        Long userId = UserHolder.getUserId();
        //查询用户信息表
        UserInfo userInfo = userInfoApi.findById(userId);
        //校验用户状态

        if (userInfo.getUserStatus().equals("2")) {
            UserStatus userStatus = userStatusApi.findByUserId(userId.intValue());
            Integer freezingRange = userStatus.getFreezingRange();
            if (freezingRange == 2) {
                return ResponseEntity.status(503).body("账户评论功能已封禁,");
            }
        }
        //封装评论数据：当前用户  对  动态publishId 发表了评论，内容是content
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());

        Publish publish=publishApi.findById(publishId);
        comment.setPublishUserId(publish.getUserId());
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());
        //   评论类型：1点赞，2评论，3喜欢
        comment.setCommentType(2);
        //   评论内容类型：1对动态评论， 2对视频评论， 3对评论评论
        comment.setPubType(1);
        //保存评论
        commentApi.save(comment);

        //大数据发送消息
        mqService.commentPublishMessage(publishId);

        return ResponseEntity.ok(null);
    }
}
